﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DecisionTree
{
    public partial class Form1 : Form
    {
        public double[][] AJT;
        TextBox textBoxMNV = new TextBox();
        TextBox textBoxMNVP = new TextBox();

        public Form1()
        {
            InitializeComponent();
            try
            {
                TreeD.readCriteria(textBoxMNV, textBoxMNVP, textBoxML, textBoxMVN);
            }
            catch
            {
            }
        }

        private void openfilebutton_Click(object sender, EventArgs e)
        {
            Boolean b = true;
            try
            {
                TreeD.mNV = 1; //minimalna ilosc wektorow potomka
                TreeD.mVN = double.Parse(textBoxMVN.Text); //minimalna wariancja wezla            
                TreeD.mL = int.Parse(textBoxML.Text); //max poziom 
                TreeD.mNVP = 1;//minimalna ilosc potomkow dla rodzica
            }
            catch (Exception ex)
            {
                b = false;
                MessageBox.Show("Zle podane dane \n" + ex.ToString());
            }

            if (b)
            {
                TreeD.loadFile(textBoxX, textBoxY);
                Methods.saveCriteria(TreeD.mNV.ToString(), TreeD.mNVP.ToString(), TreeD.mL.ToString(), TreeD.mVN.ToString());
            }
        }

        private void createTree_Click(object sender, EventArgs e) // tworzenie drzewa decyzyjnego
        {
            try
            {
                TreeD.Tree("TreeMinV", richTTree, richTVerification, Methods.minVar, Methods.verificationNode);
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Błąd przy tworzeniu drzewa \n" + ex.ToString());
            } 
        }

        private void loadTrainingSet_Click(object sender, EventArgs e)
        {
            Methods.loadFileTrain();
        }

        private void loadTree_Click(object sender, EventArgs e)
        {
            Methods.loadFileTree();
        }

        private void train_Click(object sender, EventArgs e)
        {
            AJT = Methods.train();
            richTVerification.AppendText("Nr rekordu    Nr ostatniego węzła     Wynik \n");
            for (int j = 0; j < AJT[0].Length; j++)
            {
                richTVerification.AppendText("      " + j + ")                         ");
                for (int i = 0; i < AJT.Length; i++)
                {
                    richTVerification.AppendText((AJT[i][j]).ToString() + "                           ");
                } richTVerification.AppendText("\n");
            }
            if(resultDataFileName.Text != "")
                Methods.saveDataFile(resultDataFileName, AJT);
        }

        private void reset_Click(object sender, EventArgs e)
        {
            AJT = null;
            Methods.AJ = null;
            Methods.AJTrain = null;
            Methods.AJTree = null;
            TreeD.GNN = 0;
            TreeD.L = 1;
            TreeD.N = 1;
            TreeD.P = 0;
            textBoxX.Text = "";
            textBoxY.Text = "";
            richTVerification.Clear();
            richTTree.Clear();
            GC.Collect();
        }

        private void richTTree_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
